package com.ht.api.db.hlink.function.impl;

import javax.sql.DataSource;

import com.ht.api.db.Db;
import com.ht.api.db.Entity;
import com.ht.api.db.hlink.function.HlinkFunction;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 根据给定的值查找oneid
 * @author asq
 * @createTime 2025年4月1日 10:42:53
 */
@Slf4j
public class FindOneIdFunction implements HlinkFunction<String> {

	@Override
	public String format(DataSource ds, Object value, Entity entity) {
		if (value == null) {
			log.warn("[FindOneIdFunction]传入值为null，将返回null的oneid，entity:{}", JSONUtil.toJsonStr(entity));
			return null;
		}
		
		// 从数据库查找oneid
		String sql = "select oneid from user_main where sys_deleted=1 and BITMAP_CONTAINS(users, ?) limit 1";
		int hashcode = Math.abs(value.hashCode());
		String oneid = Db.val(ds, sql, String.class, hashcode);
		return oneid;
	}

}
